<?php

namespace App\Services\MiniprogramToGame\HotSearch;

use App\Services\ServiceBase;
use App\Structs\HotSearchStruct;
use Illuminate\Support\Facades\Http;
use Goutte\Client;

class Douban extends ServiceBase
{
    public function reptile()
    {
        $client = new Client();
        $crawler = $client->request('GET', 'https://www.douban.com/gallery/');

        $list = [];
        $index = 1;
        $topics = $crawler->filter('ul.trend li')->each(function ($node) use (&$list, &$index) {
            $a         = $node->filter('a')->eq(0);
            $title     = $a->text();
            $url       = $a->attr('href');
            $hot_value = $node->filter('span')->eq(0)->text();

            foreach ([
                '万次浏览' => 10000,
                '亿次浏览' => 100000000,
                '次浏览' => 1,
            ] as $name => $per) {
                $replace = str_replace($name, "", $hot_value);
                if (strval(doubleval($replace)) == $replace) {
                    $hot_value = $replace*$per;
                    break;
                }
            }

            $class           = new HotSearchStruct;
            $class->title    = $title;
            $class->desc     = $title;
            $class->hotIndex = $index;
            $class->hotValue = $hot_value;
            $class->url      = $url;
            $list[]          = $class;
            $index++;
        });

        return $list;
    }
}
